﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System;

namespace ThridGroup.ERP.Web.Controllers.HumanResources
{
    /// <summary>
    /// 入职审核    ---tsh
    /// </summary>
    [Route("HumanResources/[action]")]
    public class OnboardingAuditsController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult OnboardingAuditsView()
        {
            // 明确指定视图路径
            return View("~/Views/HumanResources/OnboardingAuditsView.cshtml");
        }

        /// <summary>
        /// 获取入职审核数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">每页数量</param>
        /// <param name="personName">人员名称</param>
        /// <param name="interviewer">面试官</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetOnboardingAudits(int page = 1, int limit = 10, string personName = "", string interviewer = "")
        {
            try
            {
                // 模拟数据，实际应该从数据库获取
                var mockData = new List<object>();
                
                // 生成更多模拟数据以展示分页效果
                var names = new[] { "张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十", "郑十一", "王十二", "冯十三", "陈十四", "褚十五", "卫十六", "蒋十七", "沈十八", "韩十九", "杨二十", "朱二一", "秦二二", "尤二三", "许二四", "何二五", "吕二六", "施二七", "张二八", "孔二九", "曹三十" };
                var genders = new[] { "男", "女" };
                var levels = new[] { "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11" };
                var feedbacks = new[] { "还可以", "最新一轮面试...", "表现优秀", "需要改进", "符合要求" };
                var results = new[] { "入职通过", "待定", "不通过", "需要复试" };
                var interviewers = new[] { "kf1", "admin", "hr001", "manager", "director" };
                
                var random = new Random();
                for (int i = 1; i <= 45; i++)
                {
                    mockData.Add(new {
                        id = i,
                        personName = names[random.Next(names.Length)] + i,
                        phone = "158" + random.Next(10000000, 99999999).ToString(),
                        gender = genders[random.Next(genders.Length)],
                        level = levels[random.Next(levels.Length)],
                        feedbackStatus = feedbacks[random.Next(feedbacks.Length)],
                        resultStatus = results[random.Next(results.Length)],
                        interviewCount = random.Next(1, 6),
                        interviewTime = DateTime.Now.AddDays(-random.Next(0, 365)).ToString("yyyy-MM-dd"),
                        interviewer = interviewers[random.Next(interviewers.Length)]
                    });
                }

                // 根据搜索条件过滤数据
                var filteredData = mockData.Where(x => 
                    (string.IsNullOrEmpty(personName) || ((dynamic)x).personName.ToString().Contains(personName)) &&
                    (string.IsNullOrEmpty(interviewer) || ((dynamic)x).interviewer.ToString().Contains(interviewer))
                ).ToList();

                // 分页处理
                var totalCount = filteredData.Count;
                var pagedData = filteredData.Skip((page - 1) * limit).Take(limit).ToList();

                return Json(new
                {
                    code = 0,
                    msg = "success",
                    count = totalCount,
                    data = pagedData
                });
            }
            catch (System.Exception ex)
            {
                return Json(new
                {
                    code = 1,
                    msg = "获取数据失败：" + ex.Message,
                    count = 0,
                    data = new List<object>()
                });
            }
        }

        /// <summary>
        /// 删除入职审核记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteOnboardingAudit(int id)
        {
            try
            {
                // 这里应该实现实际的删除逻辑
                // await _service.DeleteAsync(id);
                
                return Json(new { success = true, message = "删除成功" });
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 导出入职审核数据
        /// </summary>
        /// <param name="personName">人员名称</param>
        /// <param name="interviewer">面试官</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportOnboardingAudits(string personName = "", string interviewer = "")
        {
            try
            {
                // 这里应该实现实际的导出逻辑
                // 生成Excel文件并返回
                
                // 模拟导出成功
                return Json(new { success = true, message = "导出成功" });
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = "导出失败：" + ex.Message });
            }
        }
    }
}
